import TdButton from '@/components/td-button'
import { confirmDel } from '@/utils'
import { deleteEnergyReport } from '@/api/energyReport'

export default {
  components: { TdButton },
  data () {
    const end = new Date().getFullYear()
    const start = 2020
    let length = end - start
    let yeasCols = []
    if (length > 0) {
      while (length) {
        yeasCols.unshift({
          label: `${start + length}年`,
          prop: `${start + length}`
        })
        length--
      }
      yeasCols.unshift({
        label: `${start}年`,
        prop: start + '',
      })
    } else if (length === 0) {
      yeasCols = [{
        label: `${start}年`,
        prop: start + '',
      }]
    }
    return {
      loading: false,
      lists: [],
      columns: [
        { label: '企业名称', prop: 'name', fixed: true },
        { label: '所属区域', prop: 'region' },
        { label: '所属行业', prop: 'industry' },
        { label: '年能耗等级', prop: 'level' },
        { label: '联系人', prop: 'contactor' },
        ...yeasCols,
      ],
    }
  },
  computed: {
    themeColor () {
      return this.$store.state.user.theme
    }
  },
  mounted () {
    this.queryLists()
  },
  methods: {
    reload () {
      this.queryLists()
    },
    viewFile (row) {
      // 预览地址
      if (row.previewUrl) {
        window.open(row.previewUrl)
      }
    },
    mapFn (v) {
      return {
        ...v,
        name: v.fileName,
        enterpriseName: v.companyName,
        // 下载地址
        url: v.fileUrl
      }
    },
    del (row) {
      confirmDel(this).then(() => {
        return deleteEnergyReport(row.id).then(res => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.reload()
          }
        })
      }).catch(() => {})
    },
    download (url, fileName, blank = true) {
      const a = document.createElement('a')
      a.href = url
      a.setAttribute('download', fileName)
      a.setAttribute('target', blank ? '_blank' : '_self')
      a.style.display = 'none'
      document.body.appendChild(a)
      a.click()
      a.parentNode.removeChild(a)
    },
  }
}
